package com.softstao.guoyu.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.accept.Accept;
import com.softstao.guoyu.base.BaseFragment;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.me.Icon;
import com.softstao.guoyu.model.sale.FromOrder;
import com.softstao.guoyu.model.sale.MonthSales;
import com.softstao.guoyu.model.sale.ToOrder;
import com.softstao.guoyu.mvp.events.NotifyEvent;
import com.softstao.guoyu.mvp.interactor.sale.OrderInteractor;
import com.softstao.guoyu.mvp.presenter.sale.OrderPresenter;
import com.softstao.guoyu.mvp.viewer.sale.MonthSalesViewer;
import com.softstao.guoyu.ui.activity.agent.AboutOrderActivity;
import com.softstao.guoyu.ui.activity.me.RebateActivity;
import com.softstao.guoyu.ui.activity.sale.FromAgentOrderListActivity;
import com.softstao.guoyu.ui.activity.sale.MyOrderListActivity;
import com.softstao.guoyu.ui.activity.sale.RetailListActivity;
import com.softstao.guoyu.ui.activity.sale.SaleRebateActivity;
import com.softstao.guoyu.ui.activity.sale.ToAgentOrderListActivity;
import com.softstao.guoyu.ui.adapter.MeIconAdapter;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.BadgeView.BadgeView;
import com.softstao.softstaolibrary.library.widget.CustomGridView;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/6/7.
 */

public class SaleFragment extends BaseFragment implements MonthSalesViewer {

    @AIPresenter(presenter = OrderPresenter.class, interactor = OrderInteractor.class)
    OrderPresenter presenter;

    @BindView(R.id.sale_amount)
    TextView saleAmount;
    @BindView(R.id.icon_view)
    CustomGridView iconView;
    @BindView(R.id.agent_order_confirm)
    LinearLayout agentOrderConfirm;
    @BindView(R.id.agent_order_shoukuan)
    LinearLayout agentOrderShoukuan;
    @BindView(R.id.agent_order_delivery)
    LinearLayout agentOrderDelivery;
    @BindView(R.id.agent_order_shouhuo)
    LinearLayout agentOrderShouhuo;
    @BindView(R.id.agent_order_cancel)
    LinearLayout agentOrderCancel;
    @BindView(R.id.order_confirm)
    LinearLayout orderConfirm;
    @BindView(R.id.order_shoukuan)
    LinearLayout orderShoukuan;
    @BindView(R.id.order_delivery)
    LinearLayout orderDelivery;
    @BindView(R.id.order_shouhuo)
    LinearLayout orderShouhuo;
    @BindView(R.id.order_cancel)
    LinearLayout orderCancel;
    private MeIconAdapter meIconAdapter;
    private List<Icon> iconList = new ArrayList<>();
    private int[] imgList = {R.mipmap.sale_icon_zhuancaigoudan, R.mipmap.me_icon_yeji, R.mipmap.me_icon_fanli,
            R.mipmap.sale_icon_lingshoudan};
    private String[] nameList = {"我的转采购单", "我的业绩", "我的返利", "我的零售单"};
    private BadgeView dot1, dot2, dot3, dot4, dot5, dot6, dot7, dot8, dot9, dot10;

    @Override
    public int _ContentView() {
        return R.layout.fragment_sale;
    }

    @Override
    public void initView() {
        setTitleBar("销售");

        addIcon();
        meIconAdapter = new MeIconAdapter(iconList);
        iconView.setAdapter(meIconAdapter);
        iconView.setOnItemClickListener((parent, view, position, id) ->
                Goto(iconList.get(position).getPic())
        );

        dot1 = new BadgeView(mContext);
        dot2 = new BadgeView(mContext);
        dot3 = new BadgeView(mContext);
        dot4 = new BadgeView(mContext);
        dot5 = new BadgeView(mContext);
        dot6 = new BadgeView(mContext);
        dot7 = new BadgeView(mContext);
        dot8 = new BadgeView(mContext);
        dot9 = new BadgeView(mContext);
        dot10 = new BadgeView(mContext);
    }

    private void addIcon() {
        for (int i = 0; i < imgList.length; i++) {
            Icon icon = new Icon();
            icon.setPic(imgList[i]);
            icon.setName(nameList[i]);
            iconList.add(icon);
        }
    }

    private void Goto(int position) {
        switch (position) {
            case R.mipmap.sale_icon_zhuancaigoudan:
                startActivity(new Intent(mContext, MyOrderListActivity.class).putExtra("orderState", ""));
                break;
            case R.mipmap.me_icon_yeji:
                startActivity(new Intent(mContext, AboutOrderActivity.class).putExtra("type", "user"));
                break;
            case R.mipmap.me_icon_fanli:
                startActivity(new Intent(mContext, RebateActivity.class));
                break;
            case R.mipmap.sale_icon_lingshoudan:
                startActivity(new Intent(mContext, RetailListActivity.class));
                break;
        }
    }

    @OnClick({R.id.agent_order, R.id.agent_order_confirm, R.id.agent_order_shoukuan, R.id.agent_order_delivery,
            R.id.agent_order_shouhuo, R.id.agent_order_cancel, R.id.all_order, R.id.order_confirm, R.id.order_shoukuan,
            R.id.order_delivery, R.id.order_shouhuo, R.id.order_cancel, R.id.sale_amount_view})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.agent_order:
                startActivity(new Intent(mContext, FromAgentOrderListActivity.class).putExtra("orderState", ""));
                break;
            case R.id.agent_order_confirm:
                startActivity(new Intent(mContext, FromAgentOrderListActivity.class).putExtra("orderState", FromOrder.UNCONFIRM + ""));
                break;
            case R.id.agent_order_shoukuan:
                startActivity(new Intent(mContext, FromAgentOrderListActivity.class).putExtra("orderState", FromOrder.UNPAY + ""));
                break;
            case R.id.agent_order_delivery:
                startActivity(new Intent(mContext, FromAgentOrderListActivity.class).putExtra("orderState", FromOrder.PAYED + ""));
                break;
            case R.id.agent_order_shouhuo:
                startActivity(new Intent(mContext, FromAgentOrderListActivity.class).putExtra("orderState", FromOrder.SHIPPED + ""));
                break;
            case R.id.agent_order_cancel:
                startActivity(new Intent(mContext, FromAgentOrderListActivity.class).putExtra("orderState", FromOrder.CANCEL + ""));
                break;
            case R.id.all_order:
                startActivity(new Intent(mContext, ToAgentOrderListActivity.class).putExtra("orderState", ""));
                break;
            case R.id.order_confirm:
                startActivity(new Intent(mContext, ToAgentOrderListActivity.class).putExtra("orderState", ToOrder.UNCONFIRM + ""));
                break;
            case R.id.order_shoukuan:
                startActivity(new Intent(mContext, ToAgentOrderListActivity.class).putExtra("orderState", ToOrder.UNPAY + ""));
                break;
            case R.id.order_delivery:
                startActivity(new Intent(mContext, ToAgentOrderListActivity.class).putExtra("orderState", ToOrder.PAYED + ""));
                break;
            case R.id.order_shouhuo:
                startActivity(new Intent(mContext, ToAgentOrderListActivity.class).putExtra("orderState", ToOrder.SHIPPED + ""));
                break;
            case R.id.order_cancel:
                startActivity(new Intent(mContext, ToAgentOrderListActivity.class).putExtra("orderState", ToOrder.CANCEL + ""));
                break;
            case R.id.sale_amount_view:
                startActivity(new Intent(mContext, SaleRebateActivity.class));
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        askMonthSales();
    }

    @Override
    public void askMonthSales() {
        presenter.getMonthSales(SharePreferenceManager.getInstance().getAgentId());
    }

    @Override
    public void getMonthSales(MonthSales monthSales) {
        if (monthSales != null) {
            saleAmount.setText(LZUtils.priceFormat(monthSales.getYearsMenoy()));
        }
    }

    @Accept
    public void notifyEvent(Object tag, NotifyEvent event) {
        int padding1 = LZUtils.dipToPix(getContext(),4);
        int padding2 = LZUtils.dipToPix(getContext(),1);
        if (event.getToOrderCount().get(0) != 0) {
            dot1.setVisibility(View.VISIBLE);
            dot1.setTargetView(agentOrderConfirm);
            dot1.setBadgeMargin(0, 3, 10, 0);
            dot1.setPadding(padding1, padding2, padding1, padding2);
            dot1.setTextColor(getResources().getColor(R.color.white));
            dot1.setTextSize(8);
            dot1.setHideOnNull(true);
            dot1.setBadgeCount(event.getToOrderCount().get(0));
        }else {
            dot1.setVisibility(View.GONE);
        }
        if (event.getToOrderCount().get(1) != 0) {
            dot2.setVisibility(View.VISIBLE);
            dot2.setTargetView(agentOrderShoukuan);
            dot2.setBadgeMargin(0, 3, 10, 0);
            dot2.setPadding(padding1, padding2, padding1, padding2);
            dot2.setTextColor(getResources().getColor(R.color.white));
            dot2.setTextSize(8);
            dot2.setHideOnNull(true);
            dot2.setBadgeCount(event.getToOrderCount().get(1));
        }else {
            dot2.setVisibility(View.GONE);
        }
        if (event.getToOrderCount().get(2) != 0) {
            dot3.setVisibility(View.VISIBLE);
            dot3.setTargetView(agentOrderDelivery);
            dot3.setBadgeMargin(0, 3, 10, 0);
            dot3.setPadding(padding1, padding2, padding1, padding2);
            dot3.setTextColor(getResources().getColor(R.color.white));
            dot3.setTextSize(8);
            dot3.setHideOnNull(true);
            dot3.setBadgeCount(event.getToOrderCount().get(2));
        }else {
            dot3.setVisibility(View.GONE);
        }
        if (event.getToOrderCount().get(3) != 0) {
            dot4.setVisibility(View.VISIBLE);
            dot4.setTargetView(agentOrderShouhuo);
            dot4.setBadgeMargin(0, 3, 10, 0);
            dot4.setPadding(padding1, padding2, padding1, padding2);
            dot4.setTextColor(getResources().getColor(R.color.white));
            dot4.setTextSize(8);
            dot4.setHideOnNull(true);
            dot4.setBadgeCount(event.getToOrderCount().get(3));
        }else {
            dot4.setVisibility(View.GONE);
        }
        if (event.getToOrderCount().get(4) != 0) {
            dot5.setVisibility(View.VISIBLE);
            dot5.setTargetView(agentOrderCancel);
            dot5.setBadgeMargin(0, 3, 10, 0);
            dot5.setPadding(padding1, padding2, padding1, padding2);
            dot5.setTextColor(getResources().getColor(R.color.white));
            dot5.setTextSize(8);
            dot5.setHideOnNull(true);
            dot5.setBadgeCount(event.getToOrderCount().get(4));
        }else {
            dot5.setVisibility(View.GONE);
        }
        if (event.getFromOrderCount().get(0) != 0) {
            dot6.setVisibility(View.VISIBLE);
            dot6.setTargetView(orderConfirm);
            dot6.setBadgeMargin(0, 3, 10, 0);
            dot6.setPadding(padding1, padding2, padding1, padding2);
            dot6.setTextColor(getResources().getColor(R.color.white));
            dot6.setTextSize(8);
            dot6.setHideOnNull(true);
            dot6.setBadgeCount(event.getFromOrderCount().get(0));
        }else {
            dot6.setVisibility(View.GONE);
        }
        if (event.getFromOrderCount().get(1) != 0) {
            dot7.setVisibility(View.VISIBLE);
            dot7.setTargetView(orderShoukuan);
            dot7.setBadgeMargin(0, 3, 10, 0);
            dot7.setPadding(padding1, padding2, padding1, padding2);
            dot7.setTextColor(getResources().getColor(R.color.white));
            dot7.setTextSize(8);
            dot7.setHideOnNull(true);
            dot7.setBadgeCount(event.getFromOrderCount().get(1));
        }else {
            dot7.setVisibility(View.GONE);
        }
        if (event.getFromOrderCount().get(2) != 0) {
            dot8.setVisibility(View.VISIBLE);
            dot8.setTargetView(orderDelivery);
            dot8.setBadgeMargin(0, 3, 10, 0);
            dot8.setPadding(padding1, padding2, padding1, padding2);
            dot8.setTextColor(getResources().getColor(R.color.white));
            dot8.setTextSize(8);
            dot8.setHideOnNull(true);
            dot8.setBadgeCount(event.getFromOrderCount().get(2));
        }else {
            dot8.setVisibility(View.GONE);
        }
        if (event.getFromOrderCount().get(3) != 0) {
            dot9.setVisibility(View.VISIBLE);
            dot9.setTargetView(orderShouhuo);
            dot9.setBadgeMargin(0, 3, 10, 0);
            dot9.setPadding(padding1, padding2, padding1, padding2);
            dot9.setTextColor(getResources().getColor(R.color.white));
            dot9.setTextSize(8);
            dot9.setHideOnNull(true);
            dot9.setBadgeCount(event.getFromOrderCount().get(3));
        }else {
            dot9.setVisibility(View.GONE);
        }
        if (event.getFromOrderCount().get(4) != 0) {
            dot10.setVisibility(View.VISIBLE);
            dot10.setTargetView(orderCancel);
            dot10.setBadgeMargin(0, 3, 10, 0);
            dot10.setPadding(padding1, padding2, padding1, padding2);
            dot10.setTextColor(getResources().getColor(R.color.white));
            dot10.setTextSize(8);
            dot10.setHideOnNull(true);
            dot10.setBadgeCount(event.getFromOrderCount().get(4));
        }else {
            dot10.setVisibility(View.GONE);
        }
    }
}
